// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Node)
    root:cc.Node=null;

    @property(cc.Prefab)
    prefab:cc.Prefab=null;

    @property(cc.Canvas)
    canvas:cc.Canvas=null;


    numberToSpan:number=20;

    spawnInterval:number=0.1;

    spawnCount:number=0;

    @property(cc.Vec2)
    randomRange:cc.Vec2=new cc.Vec2(0,0);

    // LIFE-CYCLE CALLBACKS:

     onLoad () {
         var  self=this;

         self.randomRange = cc.p(300, 200);

         self.spawnCount=0;

         self.schedule(self.addSpawn,self.spawnInterval);
        
     }

  private addSpawn(){
      //大于生成的数量
      if(this.spawnCount>=this.numberToSpan){
          
          this.clearRepeater();

          return;
      }

      let monster=cc.instantiate(this.prefab);

      monster.parent=this.root;

      monster.position=this.getRandomPosition();

      this.spawnCount++;
  }

  private clearRepeater(){

      this.unschedule(this.addSpawn);
  }

 private getRandomPosition() {
    return cc.p(cc.randomMinus1To1() * this.randomRange.x, cc.randomMinus1To1() * this.randomRange.y);
}
    // update (dt) {}
}
